import type { RouteRecordRaw } from "vue-router";

const adminRoutes: RouteRecordRaw[] = [
    {
        path: "index",
        name: "adminIndex",
        component: () => import("@/views/admin/index.vue"),
        meta: {
            title: "首页",
            roles: ["admin"],
            hidden: false,
            keepAlive: true,
        },
    },
    {
        path: "customer",
        redirect: "/layout/customer/manage",
        meta: { title: "客户管理", roles: ["admin"], hidden: false },
        children: [
            {
                path: "manage",
                name: "customerManage",
                component: () => import("@/views/admin/customer/manage.vue"),
                meta: { title: "客户列表", keepAlive: true },
            },
            {
                path: "approve",
                name: "customerApprove",
                component: () => import("@/views/admin/customer/approve.vue"),
                meta: { title: "客户注册", keepAlive: true },
            },
        ],
    },
    {
        path: "staff",
        name: "staffManage",
        component: () => import("@/views/admin/staff/index.vue"),
        meta: {
            title: "员工管理",
            roles: ["admin"],
            hidden: false,
            keepAlive: true,
        },
    },
    {
        path: "room",
        name: "roomManage",
        component: () => import("@/views/admin/room/index.vue"),
        meta: {
            title: "会议室管理",
            roles: ["admin"],
            hidden: false,
            keepAlive: true,
        },
    },
];

const customerRoutes: RouteRecordRaw[] = [
    {
        path: "index",
        name: "customerIndex",
        component: () => import("@/views/customer/index.vue"),
        meta: {
            title: "首页",
            icon: "home",
            roles: ["customer"],
            hidden: false,
            keepAlive: true,
        },
    },
    {
        path: "reserve",
        name: "reserve",
        component: () => import("@/views/customer/reserve/index.vue"),
        meta: {
            title: "会议室预约",
            roles: ["customer"],
            hidden: false,
            keepAlive: true,
        },
    },
    {
        path: "history",
        name: "history",
        component: () => import("@/views/customer/history/index.vue"),
        meta: {
            title: "预约历史",
            roles: ["customer"],
            hidden: false,
            keepAlive: true,
        },
    },
    {
        path: "payment",
        name: "payment",
        component: () => import("@/views/customer/payment/index.vue"),
        meta: {
            title: "支付",
            roles: ["customer"],
            hidden: true,
            keepAlive: false,
        },
    },
];

const staffRoutes: RouteRecordRaw[] = [
    {
        path: "index",
        name: "staffIndex",
        component: () => import("@/views/staff/index.vue"),
        meta: {
            title: "首页",
            roles: ["staff"],
            hidden: false,
            keepAlive: true,
        },
    },
    {
        path: "room",
        name: "roomManage",
        component: () => import("@/views/staff/room/index.vue"),
        meta: {
            title: "会议室管理",
            roles: ["staff"],
            hidden: false,
            keepAlive: true,
        },
    },
    {
        path: "reserve",
        redirect: "/layout/reserve/list",
        meta: { title: "预约管理", roles: ["staff"], hidden: false },
        children: [
            {
                path: "list",
                name: "reserveList",
                component: () => import("@/views/staff/reserve/list.vue"),
                meta: { title: "预约列表", keepAlive: true },
            },
            {
                path: "audit",
                name: "reserveAudit",
                component: () => import("@/views/staff/reserve/audit.vue"),
                meta: { title: "预约审核", keepAlive: true },
            },
        ],
    },
];

const sharedRoutes: RouteRecordRaw[] = [
    {
        path: "/profile",
        name: "profile",
        component: () => import("@/views/profile/index.vue"),
        meta: {
            title: "个人中心",
            roles: ["admin", "customer", "staff"],
            hidden: true,
            keepAlive: false,
        },
    },
];

export const asyncGenerateRoutes = (
    roles: "admin" | "staff" | "customer"
): RouteRecordRaw => {
    let layout: RouteRecordRaw = {
        path: "/layout",
        name: "layout",
        redirect: "/layout/index",
        component: () => import("@/layout/layout.vue"),
        children: [],
    };
    switch (roles) {
        case "admin":
            layout.children = [...adminRoutes, ...sharedRoutes];
            break;
        case "staff":
            layout.children = [...staffRoutes, ...sharedRoutes];
            break;
        case "customer":
            layout.children = [...customerRoutes, ...sharedRoutes];
            break;
    }
    return layout;
};
